package simpledb;

import java.util.*;

/**
 * The Join operator implements the relational join operation.
 */
public class Join extends Operator {

    private static final long serialVersionUID = 1L;

    private JoinPredicate joinPredicate;
    private OpIterator child1;
    private OpIterator child2;

    private Tuple tuple1=null;
    private Tuple tuple2=null;
    private TupleDesc tupleDesc;

    /**
     * Constructor. Accepts two children to join and the predicate to join them
     * on
     * 
     * @param p
     *            The predicate to use to join the children
     * @param child1
     *            Iterator for the left(outer) relation to join
     * @param child2
     *            Iterator for the right(inner) relation to join
     */
    public Join(JoinPredicate p, OpIterator child1, OpIterator child2) {
        // some code goes here
        this.joinPredicate=p;
        this.child1=child1;
        this.child2=child2;
        tupleDesc=TupleDesc.merge(
                child1.getTupleDesc(),
                child2.getTupleDesc());
        try {
            this.child1.open();
            this.child2.open();
        } catch (DbException e) {
            e.printStackTrace();
        } catch (TransactionAbortedException e) {
            e.printStackTrace();
        }

    }

    public JoinPredicate getJoinPredicate() {
        // some code goes here
        return joinPredicate;
    }

    /**
     * @return
     *       the field name of join field1. Should be quantified by
     *       alias or table name.
     * */
    public String getJoinField1Name() {
        // some code goes here
        // how to get table id? waiting to be solved.
        return "table name."+child1.getTupleDesc().getFieldName(joinPredicate.getField1());
    }

    /**
     * @return
     *       the field name of join field2. Should be quantified by
     *       alias or table name.
     * */
    public String getJoinField2Name() {
        // some code goes here
        // how to get table id? waiting to be solved.
        return "table name."+child1.getTupleDesc().getFieldName(joinPredicate.getField1());
    }

    /**
     * @see simpledb.TupleDesc#merge(TupleDesc, TupleDesc) for possible
     *      implementation logic.
     */
    @Override
    public TupleDesc getTupleDesc() {
        // some code goes here
        return TupleDesc.merge(child1.getTupleDesc(),child2.getTupleDesc());
    }

    @Override
    public void open() throws DbException, NoSuchElementException,
            TransactionAbortedException {
        // some code goes here
        super.open();
        child1.open();
        child2.open();
    }

    @Override
    public void close() {
        // some code goes here
        super.close();
        child1.close();
        child2.close();
        tuple1=null;
        tuple2=null;
    }

    @Override
    public void rewind() throws DbException, TransactionAbortedException {
        // some code goes here
        super.close();
        super.open();
        child1.rewind();
        child2.rewind();
        tuple1=null;
        tuple2=null;
    }

    /**
     * Returns the next tuple generated by the join, or null if there are no
     * more tuples. Logically, this is the next tuple in r1 cross r2 that
     * satisfies the join predicate. There are many possible implementations;
     * the simplest is a nested loops join.
     * <p>
     * Note that the tuples returned from this particular implementation of Join
     * are simply the concatenation of joining tuples from the left and right
     * relation. Therefore, if an equality predicate is used there will be two
     * copies of the join attribute in the results. (Removing such duplicate
     * columns can be done with an additional projection operator if needed.)
     * <p>
     * For example, if one tuple is {1,2,3} and the other tuple is {1,5,6},
     * joined on equality of the first column, then this returns {1,2,3,1,5,6}.
     * 
     * @return The next matching tuple.
     * @see JoinPredicate#filter
     */
    @Override
    protected Tuple fetchNext() throws TransactionAbortedException, DbException {
        // some code goes here
        while (child1.hasNext()||child2.hasNext()){
            if (tuple1==null) {
                tuple1=child1.next();
            }
            if (!child2.hasNext()){
                tuple1=child1.next();
                child2.rewind();
            }
            while (child2.hasNext()){
                tuple2=child2.next();
                if (joinPredicate.filter(tuple1,tuple2)){
                    Tuple tuple=new Tuple(tupleDesc);
                    int tupleNum1=tuple1.getTupleDesc().numFields();
                    int tupleNum2=tuple2.getTupleDesc().numFields();
                    for (int i = 0; i < tupleNum1; i++) {
                        tuple.setField(i,tuple1.getField(i));
                    }
                    for (int i = 0; i < tupleNum2; i++) {
                        tuple.setField(i+tupleNum1,tuple2.getField(i));
                    }
                    return tuple;
                }
            }
        }
        return null;
    }

    @Override
    public OpIterator[] getChildren() {
        // some code goes here
        OpIterator[]opIterators={child1,child2};
        return opIterators;
    }

    @Override
    public void setChildren(OpIterator[] children) {
        // some code goes here
        child1=children[0];
        child2=children[1];
    }

}
